const path = require('path');
const argv = require('yargs').argv;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const Pages = require('./pages.config');
let entry = {};
let html = [];
Pages.filter(page => page.publish).forEach((page) => {
  entry[`${page.url}/index`] = `./src/${page.url}/index.js`;
  html.push(new HtmlWebpackPlugin({
    template: `./src/${page.url}/index.html`,
    filename: `${page.url}/index.html`,
    chunks: [`${page.url}/index`],
    hash: true,
    cache: false
  }))
})
// console.log(argv.mode)
module.exports = {
  devServer: {
    contentBase: './dist'
  },
  entry,
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: argv.mode === 'development' ? '' : './',
    filename: '[name].js'
  },
  module: {
    rules: [
      // 处理js
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: [{
          loader: 'babel-loader'
        }]
      },
      // 处理样式文件
      {
        test: /\.(css|scss)$/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              minimize: argv.mode !== 'development',
            },
          },
          {
            loader: 'postcss-loader'
          }, {
            loader: 'sass-loader'
          }
        ]
      },
      // 处理html
      {
        test: /\.html$/,
        use: [{
          loader: 'html-loader',
          options: {
            root: path.resolve(__dirname, 'assets')
          }
        }]
      },
      {
        test: /\.ejs$/,
        loader: 'ejs-loader',
        options:{
          name:'sdsd'
        }
      },
      // 处理其他资源
      {
        test: /\.(png|jpg|jpge|gif)$/,
        use: [{
          loader: 'url-loader',
          options: {
            limit: 1024 * 8,
            name: '[path][name].[hash:5].[ext]',
            context: './src'
          }
        }]
      },
      // 处理需要挂载到window级别的类库
      {
        test: require.resolve('jquery'),
        loader: 'expose-loader?jQuery!expose-loader?$'
      }
    ]
  },
  plugins: [
    ...html,
    new MiniCssExtractPlugin({
      filename: "[name].css"
    })
  ]
}
